using System;

namespace Runtime.DesignPattern
{
    public class GameEvent<TData> : IEvent<TData>
    {
        public string EventType { get; }
        public TData EventData { get; }
        public DateTime Timestamp { get; }
        public string SourceId { get; }
        public string TargetId { get; }

        public GameEvent(string eventType, TData eventData, string sourceId, string targetId = null)
        {
            if (string.IsNullOrEmpty(eventType))
                throw new ArgumentException("Event type cannot be null or empty", nameof(eventType));

            if (string.IsNullOrEmpty(sourceId))
                throw new ArgumentException("Source ID cannot be null or empty", nameof(sourceId));

            EventType = eventType;
            EventData = eventData;
            Timestamp = DateTime.UtcNow;
            SourceId = sourceId;
            TargetId = targetId;
        }

        public override string ToString()
        {
            return $"[{Timestamp:yyyy-MM-dd HH:mm:ss.fff}] {EventType} from {SourceId} to {TargetId ?? "all"}";
        }
    }

    // 为了向后兼容，保留非泛型事件
    public class GameEvent : GameEvent<object>
    {
        public GameEvent(string eventType, object eventData, string sourceId, string targetId = null)
            : base(eventType, eventData, sourceId, targetId)
        {
        }
    }
}